package com.example.a10928.weifriend.ui.login

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_forget_pw.*

class ForgetPWActivity : BaseActivity() {

    var visible_one:Int=0
    var visible_two:Int=0

    var handler:Handler?=null
    var time:Int=300

    override fun getContentView(): Int {
        return R.layout.activity_forget_pw
    }

    override fun initView() {

        supportActionBar?.hide()
        handler=object :Handler(){
            override fun handleMessage(msg: Message?) {
                super.handleMessage(msg)
                time=time-1
                if(time>=0){
                    tv_send_code.setText("剩余"+time.toString()+"秒")
                    handler?.sendEmptyMessageDelayed(1,1000)

                }else{
                    tv_send_code.setText("发送验证码")
                }
            }
        }

        iv_pw_visible.setOnClickListener {
            if(visible_one==0){
                iv_pw_visible.setBackgroundResource(R.drawable.pw_visible)
                visible_one=1
                et_pw.inputType=145
            }else{
                iv_pw_visible.setBackgroundResource(R.drawable.pw_invisible)
                visible_one=0
                et_pw.inputType=129
            }
        }

        iv_pw_visible_two.setOnClickListener {
            if(visible_two==0){
                iv_pw_visible_two.setBackgroundResource(R.drawable.pw_visible)
                visible_two=1
                et_pw_two.inputType=145
            }else{
                iv_pw_visible_two.setBackgroundResource(R.drawable.pw_invisible)
                visible_two=0
                et_pw_two.inputType=129
            }
        }

        var textWatcher:TextWatcher=object :TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {
                change()
            }

        }
        et_phone.addTextChangedListener(textWatcher)
        et_code.addTextChangedListener(textWatcher)
        et_pw.addTextChangedListener(textWatcher)
        et_pw_two.addTextChangedListener(textWatcher)

        tv_send_code.setOnClickListener {
            if(!TextUtils.isEmpty(et_phone.text.toString())&&et_phone.text.toString().length==11){
                App.app().bpService.sendCodeSMS(et_phone.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {
                                    handler?.sendEmptyMessageDelayed(1,1000)
                                },
                                Consumer {

                                }
                        )

            }
        }

        tv_login.setOnClickListener {
            App.app().bpService.retrievePassword(et_phone.text.toString(),et_code.text.toString(),et_pw.text.toString()
            ,et_pw_two.text.toString())
                    .compose(RxUtil.io2mainLoad(this))
                    .subscribe(
                            Consumer {


                                L.t("密码修改成功")
                                finish()
                            },
                            Consumer {
                                RxUtil.errorException(it)
                            }
                    )
        }


    }

    override fun initData() {

    }


    fun change(){
        if(!TextUtils.isEmpty(et_phone.text.toString())&&!TextUtils.isEmpty(et_code.text.toString())&&
                !TextUtils.isEmpty(et_pw.text.toString())&&!TextUtils.isEmpty(et_pw_two.text.toString())){
            tv_login.setBackgroundResource(R.drawable.input)
        }else{
            tv_login.setBackgroundResource(R.drawable.uninput)
        }
    }

}
